package com.weixx.yctp.cp.download;

import cc.eamon.open.permission.Limit;
import cc.eamon.open.permission.annotation.Permission;
import cc.eamon.open.permission.annotation.PermissionLimit;
import cc.eamon.open.status.StatusBaseController;
import cc.eamon.open.status.StatusException;
import com.weixx.yctp.cp.entity.Cp;
import com.weixx.yctp.cp.entity.CpSimpleMapper;
import com.weixx.yctp.cp.service.CpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(
        value="测评-下载",
        tags = "测评-下载"
)
@RestController
@RequestMapping("/down")
public class TestController extends StatusBaseController {

    @Autowired
    private CpService cpService;

    /**
     * Excel表格导出接口
     * http://localhost:8080/ExcelDownload
     *
     *
     * @param response response对象
     * @throws IOException 抛IO异常
     */
    @ApiOperation(value = "批量下载", tags = "根据时间批量下载", produces = "application/octet-stream")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/time.xlsx", method = RequestMethod.GET)
    @PermissionLimit(limits = Limit.OPERATION_RETRIEVE)
    public void excelDownload(@RequestParam String start, @RequestParam String end, HttpServletResponse response) throws IOException, StatusException, ParseException {
        DownUtil util=new DownUtil();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate=sdf.parse(start);
        Date endDate=sdf.parse(end);
    util.excelDownloadUtile(cpService.queryByTime(startDate, endDate, (long) -1, 10), response, true);

    }

    @ApiOperation(value = "大批量下载", tags = "根据批次批量下载", produces = "application/octet-stream")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/PcTime.xlsx", method = RequestMethod.GET)
    @PermissionLimit(limits = Limit.OPERATION_RETRIEVE)
    public void excelDownloadBy(@RequestBody List<String> PcTime, HttpServletResponse response) throws IOException, StatusException, ParseException {
        DownUtil util=new DownUtil();
        Map<String, List<Map<String, Object>>> stringListMap = cpService.queryByPcMutil(PcTime, (long) -1, 10);
        for (String s : PcTime) {
            System.out.println(s);
            util.excelDownloadUtile(stringListMap.get(s),response,true);
        }
    }

    @ApiOperation(value = "平均测评值下载", tags = "平均测评值下载", produces = "application/octet-stream")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/avg.xlsx", method = RequestMethod.GET)
    @PermissionLimit(limits = Limit.OPERATION_RETRIEVE)
    public void excelDownloadAVG(@RequestParam String Pctime, HttpServletResponse response) throws IOException, StatusException, ParseException {
        DownUtil util=new DownUtil();
        List<Map.Entry<String, Integer>> entries = cpService.queryByPcAverage(Pctime);
        List<Map<String,Object>> give=new ArrayList<>();
        Map<String,Object> inner= new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : entries) {
            inner.put(entry.getKey(),entry.getValue());
        }
        give.add(inner);
util.excelDownloadUtile(give, response, false);
    }


    @ApiOperation(value = "根据记录id下载", tags = "根据创建时间下载", produces = "application/octet-stream")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/avg/byprimaykey.xlsx", method = RequestMethod.GET)
    @PermissionLimit(limits = Limit.OPERATION_RETRIEVE)
    public void excelDownloadAVG(@RequestParam Integer downId, HttpServletResponse response) throws IOException, StatusException, ParseException {
        DownUtil util=new DownUtil();
        Cp cpByPrimaryKey = cpService.getCpByPrimaryKey(downId);
        Map<String, Object> map = CpSimpleMapper.getMap(cpByPrimaryKey);
        List<Map<String,Object>> entityMapList=new ArrayList<>();
        entityMapList.add(map);
 util.excelDownloadUtile(entityMapList, response, true);

    }

}